package com.education.basic.dao.impl;


import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.education.basic.dao.DepartmentDao;
import com.education.basic.entity.Department;
import com.yunshangwoniu.core.baseDao.impl.BaseDaoImpl;

@Component("departmentDao")
public class DepartmentDaoImpl extends BaseDaoImpl<Department> implements DepartmentDao
{
	@Resource
	private SessionFactory sessionFactory;
	
	@Override
	@Transactional
	public String getLastCode()
	{
		Session session = sessionFactory.getCurrentSession();
		String hql = "From Department d order by d.createTime desc";
		Query query = session.createQuery(hql).setFirstResult(0).setMaxResults(1);
		Object obj = query.uniqueResult();
		return obj==null?null:((Department)obj).getCode();
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional
	public List<Department> queryAll()
	{
		Session session = sessionFactory.getCurrentSession();
		String hql = "From Department d order by d.createTime desc";
		Query q = session.createQuery(hql);
		return (List<Department>)q.list();
	}
	
}
